/**
 * 
 */
package com.manthano.util.validation;

import java.util.regex.Pattern;

/**
 * @author Dell
 */
public class PasswordUtil
{
	private static int	passwordScore	= 0;

	/**
	 * @param password
	 * @param userName
	 * @return score
	 */
	public static int getPasswordStrength(String password, String userName)
	{
		if (password.length() < 4)
		{
			passwordScore += 5;
		}
		else if ((password.length() >= 4) && (password.length() <= 8))
		{
			passwordScore += 10;
		}
		else if ((password.length() >= 9) && (password.length() <= 14))
		{
			passwordScore += 15;
		}
		else if (password.length() >= 15)
		{
			passwordScore += 20;
		}

		Pattern p = Pattern.compile("[a-z]");
		if (p.matcher(password).find())
		{
			passwordScore += 1;
		}

		Pattern p2 = Pattern.compile("[A-Z]");
		if (p2.matcher(password).find())
		{
			passwordScore += 3;
		}

		Pattern p3 = Pattern.compile("[0-9]");
		if (p3.matcher(password).find())
		{
			passwordScore += 5;
		}

		Pattern p4 = Pattern.compile("[!,@,#,$,%,^,&,*,?,_,~]");
		if (p4.matcher(password).find())
		{
			passwordScore += 7;
		}

		Pattern p5 = Pattern.compile("([a-z].*[A-Z])|([A-Z].*[a-z])");
		if (p5.matcher(password).find())
		{
			passwordScore += 5;
		}

		Pattern p6 = Pattern.compile("([a-zA-Z].*[0-9])|([0-9].*[a-zA-Z])");
		if (p6.matcher(password).find())
		{
			passwordScore += 7;
		}

		Pattern p7 = Pattern.compile("([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])");
		if (p7.matcher(password).find())
		{
			passwordScore += 10;
		}

		Pattern p8 = Pattern.compile(userName);
		if (p8.matcher(password).find())
		{
			passwordScore = 5;
		}
		return passwordScore;
	}
}
